<template>
  <div style="padding: 0px 100px 0px 100px" class="app-container">
    <Layout />
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="roleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column width="55" align="center" />
      <el-table-column label="问题性质编号" prop="id" width="120" />
      <el-table-column
        label="问题编号"
        prop="questionId"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="章节"
        prop="chapter"
        :show-overflow-tooltip="true"
        width="120"
      />
      <el-table-column
        label="易错点"
        prop="falliblePoint"
        :show-overflow-tooltip="true"
        width="120"
      />
      <el-table-column
        label="答案"
        prop="answer"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="答案解析"
        prop="explanation"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="出错率"
        prop="wrongRate"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="问题id" prop="questionId">
          <el-input v-model="form.questionId" placeholder="请输入问题id" />
        </el-form-item>
        <el-form-item label="章节" prop="chapter">
          <el-input v-model="form.chapter" placeholder="请输入章节" />
        </el-form-item>
        <el-form-item label="答案" prop="answer">
          <el-input v-model="form.answer" placeholder="请输入答案" />
        </el-form-item>
        <el-form-item label="答案解析" prop="explanation">
          <el-input v-model="form.explanation" placeholder="答案解析" />
        </el-form-item>
        <el-form-item label="易错点" prop="falliblePoint">
          <el-input v-model="form.falliblePoint" placeholder="易错点" />
        </el-form-item>
        <el-form-item label="出错率" prop="wrongRate">
          <el-input v-model="form.wrongRate" placeholder="请输入出错率" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Layout from '@/layout'
import { addProperty ,updateProperty ,getProperty, listProperty, deleteProperty} from '@/api/problemProperty'
import Pagination from "@/components/Pagination";
export default {
  name: 'problemProperty',
  components: {
    Layout,
    Pagination
   },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      scoreOpen: false,
      scores: {},

      knowledgeOpen: false,
      knowledge: [],
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        roleName: undefined,
        roleKey: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "学生年龄不能为空", trigger: "blur" }],
        questionId: [
          { required: true, message: "问题id不能为空", trigger: "blur" },
        ],
        chapter: [
          { required: true, message: "章节不能为空", trigger: "blur" },
        ],
        answer: [
          { required: true, message: "答案不能为空", trigger: "blur" },
        ],
        explanation: [{ required: true, message: "答案解释不能为空", trigger: "blur" }],
        falliblePoint: [{ required: true, message: "易错点不能为空", trigger: "blur" }],
        wrongRate: [{ required: true, message: "出错率不能为空", trigger: "blur" }],
      },
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  // computed: {
  //   totalPage () { // 总页数
  //     return Math.ceil(this.total / this.pageSize) // 向上取整
  //   },
  //   pageList () { // 获取显示的页码数组
  //     return this.dealPageList(this.currentPage).filter(n => n !== 1 && n !== this.totalPage)
  //   }
  // },
  // watch: {
  //   current (to) { // 当前选中页码
  //     this.currentPage = to
  //   },
  //   currentPage (to) { // 通过更改当前页码，修改分页数据
  //     this.loading = true
  //     this.$emit('change', {
  //       page: to,
  //       pageSize: this.pageSize
  //     })
  //   }
  // },
  created() {
    this.getList();
  },
  methods: {
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            console.log(11111, this.form.id);
            updateProperty(this.form)
              .then((response) => {
                if (response.success) {
                  this.$alert("修改成功", {
                    confirmButtonText: "修改成功",
                    callback: (action) => {
                      // console.log('请检查后重新输入')
                    },
                  });
                  this.open = false;
                  this.getList();
                } else {
                  throw new Error("出现错误", response.message);
                }
              })
              .catch((e) => {
                this.$alert("出现错误", {
                  confirmButtonText: e + "\n" + "请检查后重新输入",
                  callback: (action) => {
                    console.log("请检查后重新输入");
                  },
                });
              });
          } else {
            console.log(22222, this.form.roleId);
            // this.form.menuIds = this.getMenuAllCheckedKeys();
            addProperty(this.form)
              .then((response) => {
                if (response.success) {
                  this.$alert("新增成功", {
                    confirmButtonText: "新增成功",
                    callback: (action) => {
                      // console.log('请检查后重新输入')
                    },
                  });
                  this.open = false;
                  this.getList();
                } else {
                  throw new Error("出现错误", response.message);
                }
              })
              .catch((e) => {
                this.$alert("出现错误", {
                  confirmButtonText: e + "\n" + "请检查后重新输入",
                  callback: (action) => {
                    console.log("请检查后重新输入");
                  },
                });
              });
          }
        }
      });
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      listProperty()
        .then((response) => {
          if (response.success) {
            this.roleList = response.data;
            this.total = response.data.length;
            this.loading = false;
          }
        })
        .catch((e) => {
          _this.$message.error(e, "出现未知错误");
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.form = {
        questionId: undefined,
        chapter: undefined,
        falliblePoint: undefined,
        answer: undefined,
        explanation: undefined,
        wrongRate: undefined
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.getMenuTreeselect();
      this.open = true;
      this.title = "添加题目性质";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log("修改按钮操作");
      this.reset();
      this.roleList.forEach((item) => {
        if (item.id == row.id) {
          this.form = item;
          this.open = true;
          this.title = "修改题目性质";
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if (row.id) {
        this.$modal
          .confirm('是否确认删除学生编号为"' + row.id + '"的数据项？')
          .then(function () {
            return deleteProperty({
              id: row.id,
            });
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      } else {
        this.$alert("出现错误", {
          confirmButtonText: "请选择要删除的人",
          callback: (action) => {
            console.log("请检查后重新输入");
          },
        });
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.auto-generate-form {
  padding-top: 50px;
  padding-right: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
</style>
